/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tcloudclient.fileSystem;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import tcloudclient.watchService.WatchServiceChangeData;
import tcloudclient.watchService.WatchServiceChangeType;
import tcloudclient.watchService.WatchServiceEventInterface;

/**
 *
 * @author Tomas
 */
public class FileSystemWachtServiceListener implements WatchServiceEventInterface {

    private FileNode root;
    private List<FileSystemStatusDataChangeLintenerInterface> dataChangeObservers;

    public FileSystemWachtServiceListener(FileNode root) {
        this.root = root;
        dataChangeObservers = new ArrayList<FileSystemStatusDataChangeLintenerInterface>();
    }

    @Override
    public void event(WatchServiceChangeData data) {
        String folder = data.getFolder();
        WatchServiceChangeType operationType = data.getOperationType();
        FileNode findedNode = FileSystemUtils.findFileNode(folder, root);
        if (operationType == WatchServiceChangeType.RENAME) {
            String oldName = findedNode.getName();
            findedNode.setFile(new File(root.getFile().getAbsolutePath() + File.separator + data.getNewFolderName()));
            findedNode.setLastChange(findedNode.getFile().lastModified());
            if (findedNode.getStatus() != FileStatusEnum.NEW && findedNode.getStatus() != FileStatusEnum.RENAME) {
                findedNode.setStatus(FileStatusEnum.RENAME);
                findedNode.setOldName(oldName);
            }
        }

        if (operationType == WatchServiceChangeType.CREATE) {
            File file = new File(root.getFile().getAbsolutePath() + File.separator + folder);
            findedNode.getChilds().add(new FileNode(file, FileStatusEnum.NEW, new Date(file.lastModified()), new ArrayList<FileNode>()));
        }

        if (operationType == WatchServiceChangeType.MODIFY) {
            if (findedNode.getStatus() != FileStatusEnum.NEW && findedNode.getStatus() != FileStatusEnum.RENAME) {
                findedNode.setStatus(FileStatusEnum.MODIFY);
            }
            findedNode.setLastChange(findedNode.getFile().lastModified());
        }

        if (operationType == WatchServiceChangeType.DELETE) {
            if (findedNode.getStatus() == FileStatusEnum.NEW) {
                FileNode parentNode = FileSystemUtils.findParentNode(folder, root);
                parentNode.getChilds().remove(findedNode);
            } else {
                findedNode.setStatus(FileStatusEnum.DELETE);
            }
        }
        notifyAllDataChangeObservers();
    }



    public FileNode getRoot() {
        return root;
    }

    public void addDataChangeObserver(FileSystemStatusDataChangeLintenerInterface dataChangeObserver) {
        dataChangeObservers.add(dataChangeObserver);
    }

    public void removeDataChangeObserver(FileSystemStatusDataChangeLintenerInterface dataChangeObserver){
        dataChangeObservers.remove(dataChangeObserver);
    }
    
    private void notifyAllDataChangeObservers() {
        for (FileSystemStatusDataChangeLintenerInterface fileSystemStatusDataChange : dataChangeObservers) {
            fileSystemStatusDataChange.dataChangeEventHandler(root);
        }
    }
}
